const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');//抽离css

// 根据不同环境取用不同参数
const {env} = process.env.NODE_ENV === 'production' 
? require('./webpack.prod') 
: require('./webpack.dev') 

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'build.js',
    path: path.join(__dirname, 'dist')
  },
  mode: env,
  module: {
    rules: [
      {
        test: /\.css$/, 
        use:['style-loader','css-loader']
      },
      {
        test: /\.less$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader','less-loader']
      },
      {
        test: /\.js$/,
        loader: 'babel-loader', 
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        loader: 'eslint-loader',
        enforce: "pre",
        include: [path.resolve(__dirname, 'src')], // 指定检查的目录
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.(png|jpg)$/,
        loader: 'url-loader',
        options: {
          limit: 5*1024,
          name: `images/[hash:10].[ext]`,
          esModule: false,
        }
      }
    
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: 'cache',
      url: './public/favicon.ico',
      template: './public/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: 'main.css',
    }),
    new webpack.HotModuleReplacementPlugin()
  ],
  devServer: {
    contentBase: './dist',
    open: true,
    hot: true
  }
}